import 'package:flutter_app/bean/list_app_article_entity.dart';
import 'package:flutter_app/network/Api.dart';
import 'package:flutter_app/network/ResultData.dart';
import 'package:flutter_app/ui/news/news_detail/news_detail_view.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:get/get.dart';

import 'news_state.dart';

class NewsLogic extends GetxController {
  final NewsState state = NewsState();

  @override
  void onReady() {
    super.onReady();
    postListArticle(state.currentPage.value);
  }


  /// 门户-获取内容分页信息
  Future<void> postListArticle (int currentPage) async {
    Map<String, dynamic> params  = {
      "contentType": "XNYW",
      "isRotation": "",
      "keyword": "",
      "state": ""
    };

    Map<String, dynamic> param = {
      "currentPage": currentPage,
      'pageSize': 15,
      'param': params
    };
    ResultData resultData = await Api.postListArticle(param);
    if (resultData.isSuccess) {
      if (currentPage == 1) {
        state.listAppArticleEntity = ListAppArticleEntity.fromJson(resultData.data['data']);
      } else if (currentPage > 1) {
        state.listAppArticleEntity!.content!.addAll(ListAppArticleEntity.fromJson(resultData.data['data']).content!);
      }
      update();
    } else {
      toast.show(resultData.data["message"]);
    }
  }

  goToNewsDetailPage(String? id) => Get.to(() => NewsDetailPage(title: '要闻详情',), arguments: id);

  @override
  void onClose() {
    super.onClose();
  }
}
